/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.fba.prionace.file.JFile;

public class PathBar extends JPanel implements AdjustmentListener,
		MouseListener, Runnable {

	private JScrollPane scroll;

	private JPanel path;

	private ButtonGroup group;

	private ArrayList<JFile> pathButtons;

	private ArrayList<JButton> scrollButtons;

	private int scrollValue;

	public PathBar() {
		super();
		init();
	}

	private void init() {
		path = new JPanel(new FlowLayout(FlowLayout.LEFT));
		group = new ButtonGroup();
		pathButtons = new ArrayList<JFile>();
		scrollButtons = new ArrayList<JButton>();

		scroll = new JScrollPane(path);
		scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scroll.getHorizontalScrollBar().addAdjustmentListener(this);
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setViewportBorder(null);

		setLayout(new BorderLayout());
		add(scroll, BorderLayout.CENTER);
	}

	public ArrayList<JFile> getPathButtons() {
		return pathButtons;
	}

	public File getCurrentPath() {
		for (JFile currentPath : pathButtons) {
			if (currentPath.isSelected()) {
				return currentPath.getFile();
			}
		}
		return null;
	}

	public void setCurrentPath(File currentPath) {
		for (JFile file : pathButtons) {
			group.remove(file);
		}

		for (Component c : path.getComponents()) {
			path.remove(c);
		}

		JFile pathButton = new JFile();
		pathButton.setFile(currentPath);

		do {
			pathButton.setSelected(true);
			group.add(pathButton);
			pathButtons.add(0, pathButton);
			path.add(pathButton, 0);
			pathButton = pathButton.getParentFile(false);
		} while (pathButton != null);

		firePathChanged();
		revalidate();
		repaint();
	}

	public void registerScrollButton(JButton scrollButton) {
		scrollButtons.add(scrollButton);
	}

	public void unregisterScrollButton(JButton scrollButton) {
		scrollButtons.remove(scrollButton);
	}

	public void addPathChangeListener(PathChangeListener listener) {
		listenerList.add(PathChangeListener.class, listener);
	}

	public void removePathChangeListener(PathChangeListener listener) {
		listenerList.remove(PathChangeListener.class, listener);
	}

	public PathChangeListener[] getPathChangeListeners() {
		return listenerList.getListeners(PathChangeListener.class);
	}

	public void firePathChanged() {
		PathChangeListener[] listeners = getPathChangeListeners();
		for (PathChangeListener listener : listeners) {
			PathChangeEvent event = new PathChangeEvent(this);
			listener.pathChanged(event);
		}
	}

	public void adjustmentValueChanged(AdjustmentEvent e) {
		boolean scrollButtonsVisible = path.getSize().width > scroll.getSize().width;

		for (JButton scrollButton : scrollButtons) {
			scrollButton.setVisible(scrollButtonsVisible);
			scrollButton.getParent().validate();
			scrollButton.getParent().repaint();
		}
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		JButton source = (JButton) e.getSource();

		if (source.getActionCommand().equals("scroll left")) {
			scrollValue = -1;
		} else {
			scrollValue = 1;
		}
		Thread scroll = new Thread(this);
		scroll.start();
	}

	public void mouseReleased(MouseEvent e) {
		scrollValue = 0;
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void run() {
		while (scrollValue != 0) {
			scroll.getHorizontalScrollBar().setValue(
					scroll.getHorizontalScrollBar().getValue() + scrollValue);
		}
	}
}
